/*      */ package com.zimbra.qa.unittest.prov.ldap;
/*      */ 
/*      */ import com.google.common.collect.Lists;
/*      */ import com.google.common.collect.Maps;
/*      */ import com.zimbra.common.account.Key.CosBy;
/*      */ import com.zimbra.common.account.ZAttrProvisioning.CalResType;
/*      */ import com.zimbra.common.service.ServiceException;
/*      */ import com.zimbra.cs.account.Account;
/*      */ import com.zimbra.cs.account.Alias;
/*      */ import com.zimbra.cs.account.CalendarResource;
/*      */ import com.zimbra.cs.account.Cos;
/*      */ import com.zimbra.cs.account.DistributionList;
/*      */ import com.zimbra.cs.account.Domain;
/*      */ import com.zimbra.cs.account.DynamicGroup;
/*      */ import com.zimbra.cs.account.Group;
/*      */ import com.zimbra.cs.account.MailTarget;
/*      */ import com.zimbra.cs.account.NamedEntry;
/*      */ import com.zimbra.cs.account.NamedEntry.Visitor;
/*      */ import com.zimbra.cs.account.Provisioning;
/*      */ import com.zimbra.cs.account.SearchAccountsOptions;
/*      */ import com.zimbra.cs.account.SearchAccountsOptions.IncludeType;
/*      */ import com.zimbra.cs.account.SearchDirectoryOptions;
/*      */ import com.zimbra.cs.account.SearchDirectoryOptions.MakeObjectOpt;
/*      */ import com.zimbra.cs.account.SearchDirectoryOptions.ObjectType;
/*      */ import com.zimbra.cs.account.SearchDirectoryOptions.SortOpt;
/*      */ import com.zimbra.cs.account.Server;
/*      */ import com.zimbra.cs.account.Signature;
/*      */ import com.zimbra.cs.account.ldap.LdapDIT;
/*      */ import com.zimbra.cs.account.ldap.LdapProv;
/*      */ import com.zimbra.cs.account.ldap.entry.LdapDomain;
/*      */ import com.zimbra.cs.ldap.LdapUtil;
/*      */ import com.zimbra.cs.ldap.ZLdapFilter;
/*      */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*      */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*      */ import com.zimbra.qa.QA.Bug;
/*      */ import com.zimbra.qa.unittest.TestUtil;
/*      */ import com.zimbra.qa.unittest.prov.Names;
/*      */ import com.zimbra.qa.unittest.prov.ProvTest.SkipTestReason;
/*      */ import com.zimbra.qa.unittest.prov.ProvTest.SkippedForInMemLdapServerException;
/*      */ import com.zimbra.qa.unittest.prov.Verify;
/*      */ import java.util.Collections;
/*      */ import java.util.HashMap;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import org.junit.AfterClass;
/*      */ import org.junit.Assert;
/*      */ import org.junit.BeforeClass;
/*      */ import org.junit.Test;
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ public class TestLdapProvSearchDirectory
/*      */   extends LdapTest
/*      */ {
/*      */   private static LdapProvTestUtil provUtil;
/*      */   private static Provisioning prov;
/*      */   private static Domain domain;
/*      */   
/*      */   @BeforeClass
/*      */   public static void init()
/*      */     throws Exception
/*      */   {
/*   75 */     provUtil = new LdapProvTestUtil();
/*   76 */     prov = provUtil.getProv();
/*   77 */     domain = provUtil.createDomain(baseDomainName(), null);
/*      */   }
/*      */   
/*      */   @AfterClass
/*      */   public static void cleanup() throws Exception {
/*   82 */     Cleanup.deleteAll(new String[] { baseDomainName() });
/*      */   }
/*      */   
/*      */   private Account createAccount(Provisioning prov, String localPart, Domain domain, Map<String, Object> attrs) throws Exception
/*      */   {
/*   87 */     return provUtil.createAccount(localPart, domain, attrs);
/*      */   }
/*      */   
/*      */   private Account createAccount(String localPart, Map<String, Object> attrs) throws Exception
/*      */   {
/*   92 */     return createAccount(prov, localPart, domain, attrs);
/*      */   }
/*      */   
/*      */   private Account createAccount(String localPart) throws Exception {
/*   96 */     return createAccount(prov, localPart, domain, null);
/*      */   }
/*      */   
/*      */   private void deleteAccount(Account acct) throws Exception {
/*  100 */     provUtil.deleteAccount(acct);
/*      */   }
/*      */   
/*      */   private CalendarResource createCalendarResource(String localPart, Domain domain, Map<String, Object> attrs) throws Exception
/*      */   {
/*  105 */     return provUtil.createCalendarResource(localPart, domain, attrs);
/*      */   }
/*      */   
/*      */   private CalendarResource createCalendarResource(String localPart) throws Exception {
/*  109 */     return createCalendarResource(localPart, domain, null);
/*      */   }
/*      */   
/*      */   private DistributionList createDistributionList(String localPart, Domain domain, Map<String, Object> attrs) throws Exception
/*      */   {
/*  114 */     return provUtil.createDistributionList(localPart, domain, attrs);
/*      */   }
/*      */   
/*      */   private DistributionList createDistributionList(String localPart, Domain domain) throws Exception
/*      */   {
/*  119 */     return createDistributionList(localPart, domain, null);
/*      */   }
/*      */   
/*      */   private DistributionList createDistributionList(String localPart) throws Exception
/*      */   {
/*  124 */     return createDistributionList(localPart, domain, null);
/*      */   }
/*      */   
/*      */   private DynamicGroup createDynamicGroup(String localPart, Domain domain) throws Exception {
/*  128 */     return provUtil.createDynamicGroup(localPart, domain, null);
/*      */   }
/*      */   
/*      */   private DynamicGroup createDynamicGroup(String localPart) throws Exception {
/*  132 */     return createDynamicGroup(localPart, domain);
/*      */   }
/*      */   
/*      */   private void deleteGroup(Group group) throws Exception {
/*  136 */     provUtil.deleteGroup(group);
/*      */   }
/*      */   
/*      */   private Server createServer(String serverName, Map<String, Object> attrs) throws Exception
/*      */   {
/*  141 */     return provUtil.createServer(serverName, attrs);
/*      */   }
/*      */   
/*      */   private Server createServer(String serverName) throws Exception
/*      */   {
/*  146 */     return provUtil.createServer(serverName, null);
/*      */   }
/*      */   
/*      */   private void deleteServer(Server server) throws Exception {
/*  150 */     provUtil.deleteServer(server);
/*      */   }
/*      */   
/*      */   private void deleteDomain(Domain domain) throws Exception {
/*  154 */     provUtil.deleteDomain(domain);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void searchAccountsOnServer()
/*      */     throws Exception
/*      */   {
/*  161 */     String DOMAIN_NAME = genDomainSegmentName() + "." + baseDomainName();
/*  162 */     Domain searchDomain = provUtil.createDomain(DOMAIN_NAME, null);
/*      */     
/*      */ 
/*  165 */     String ACCT_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart("acct"));
/*  166 */     String CR_LOCALPART = Names.makeAccountNameLocalPart(genAcctNameLocalPart("cr"));
/*      */     
/*  168 */     Map<String, Object> crAttrs = Maps.newHashMap();
/*  169 */     crAttrs.put("displayName", "ACCT_LOCALPART");
/*  170 */     crAttrs.put("zimbraCalResType", ZAttrProvisioning.CalResType.Equipment.name());
/*  171 */     Account acct = createAccount(prov, ACCT_LOCALPART, searchDomain, null);
/*  172 */     CalendarResource cr = createCalendarResource(CR_LOCALPART, searchDomain, crAttrs);
/*      */     
/*  174 */     Server server = prov.getLocalServer();
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*  179 */     SearchAccountsOptions opts = new SearchAccountsOptions(searchDomain, new String[] { "zimbraId" });
/*  180 */     opts.setMakeObjectOpt(SearchDirectoryOptions.MakeObjectOpt.NO_DEFAULTS);
/*  181 */     List<NamedEntry> result = prov.searchAccountsOnServer(server, opts);
/*  182 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct, cr }), result, false);
/*      */     
/*      */ 
/*  185 */     boolean caughtTooManySearchResultsException = false;
/*      */     try {
/*  187 */       opts = new SearchAccountsOptions(searchDomain, new String[] { "zimbraId" });
/*  188 */       opts.setMakeObjectOpt(SearchDirectoryOptions.MakeObjectOpt.NO_DEFAULTS);
/*  189 */       opts.setMaxResults(1);
/*  190 */       result = prov.searchAccountsOnServer(server, opts);
/*      */     } catch (ServiceException e) {
/*  192 */       if ("account.TOO_MANY_SEARCH_RESULTS".equals(e.getCode())) {
/*  193 */         caughtTooManySearchResultsException = true;
/*      */       }
/*      */     }
/*  196 */     Assert.assertTrue(caughtTooManySearchResultsException);
/*      */     
/*      */ 
/*      */ 
/*  200 */     opts = new SearchAccountsOptions(searchDomain, new String[] { "zimbraId" });
/*  201 */     opts.setMakeObjectOpt(SearchDirectoryOptions.MakeObjectOpt.NO_DEFAULTS);
/*  202 */     opts.setIncludeType(SearchAccountsOptions.IncludeType.ACCOUNTS_ONLY);
/*  203 */     result = prov.searchAccountsOnServer(server, opts);
/*  204 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct }), result, false);
/*      */     
/*      */ 
/*  207 */     opts = new SearchAccountsOptions(searchDomain, new String[] { "zimbraId" });
/*  208 */     opts.setMakeObjectOpt(SearchDirectoryOptions.MakeObjectOpt.NO_DEFAULTS);
/*  209 */     opts.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_DESCENDING);
/*  210 */     result = prov.searchAccountsOnServer(server, opts);
/*  211 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { cr, acct }), result, true);
/*      */     
/*  213 */     opts = new SearchAccountsOptions(searchDomain, new String[] { "zimbraId" });
/*  214 */     opts.setMakeObjectOpt(SearchDirectoryOptions.MakeObjectOpt.NO_DEFAULTS);
/*  215 */     opts.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  216 */     result = prov.searchAccountsOnServer(server, opts);
/*  217 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct, cr }), result, true);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void getAllAccounts() throws Exception {
/*  222 */     Account acct3 = createAccount(genAcctNameLocalPart("3"));
/*  223 */     Account acct2 = createAccount(genAcctNameLocalPart("2"));
/*  224 */     Account acct1 = createAccount(genAcctNameLocalPart("1"));
/*  225 */     CalendarResource cr = createCalendarResource(genAcctNameLocalPart("cr"));
/*      */     
/*  227 */     List<NamedEntry> accounts = prov.getAllAccounts(domain);
/*  228 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct1, acct2, acct3 }), accounts, true);
/*      */     
/*      */ 
/*  231 */     final List<NamedEntry> acctsByVisitor = Lists.newArrayList();
/*  232 */     NamedEntry.Visitor visitor = new NamedEntry.Visitor()
/*      */     {
/*      */       public void visit(NamedEntry entry) throws ServiceException
/*      */       {
/*  236 */         acctsByVisitor.add(entry);
/*      */       }
/*  238 */     };
/*  239 */     prov.getAllAccounts(domain, visitor);
/*  240 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct1, acct2, acct3 }), accounts, false);
/*      */     
/*  242 */     deleteAccount(acct1);
/*  243 */     deleteAccount(acct2);
/*  244 */     deleteAccount(acct3);
/*  245 */     deleteAccount(cr);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void getAllAccountsByDomainAndServer() throws Exception {
/*  250 */     Account acct3 = createAccount(genAcctNameLocalPart("3"));
/*  251 */     Account acct2 = createAccount(genAcctNameLocalPart("2"));
/*  252 */     Account acct1 = createAccount(genAcctNameLocalPart("1"));
/*  253 */     CalendarResource cr = createCalendarResource(genAcctNameLocalPart("cr"));
/*      */     
/*  255 */     final List<NamedEntry> acctsVisitor = Lists.newArrayList();
/*  256 */     NamedEntry.Visitor visitor = new NamedEntry.Visitor()
/*      */     {
/*      */       public void visit(NamedEntry entry) throws ServiceException {
/*  259 */         acctsVisitor.add(entry);
/*      */       }
/*      */       
/*  262 */     };
/*  263 */     Server server = prov.getLocalServer();
/*  264 */     prov.getAllAccounts(domain, server, visitor);
/*  265 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct1, acct2, acct3 }), acctsVisitor, false);
/*      */     
/*  267 */     acctsVisitor.clear();
/*  268 */     Server otherServer = createServer(genServerName());
/*  269 */     prov.getAllAccounts(domain, otherServer, visitor);
/*  270 */     Verify.verifyEquals(null, acctsVisitor, false);
/*      */     
/*  272 */     deleteAccount(acct1);
/*  273 */     deleteAccount(acct2);
/*  274 */     deleteAccount(acct3);
/*  275 */     deleteAccount(cr);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void accountsByGrants() throws Exception
/*      */   {
/*  281 */     String GRANTEE_ID_1 = LdapUtil.generateUUID();
/*  282 */     String GRANTEE_ID_2 = LdapUtil.generateUUID();
/*  283 */     String GRANTEE_ID_3 = LdapUtil.generateUUID();
/*  284 */     List<String> GRANTEE_IDS = Lists.newArrayList(new String[] { GRANTEE_ID_1, GRANTEE_ID_2, GRANTEE_ID_3 });
/*      */     
/*  286 */     Map<String, Object> attrs1 = Maps.newHashMap();
/*  287 */     attrs1.put("zimbraSharedItem", "granteeId:" + GRANTEE_ID_3 + "blah blah");
/*  288 */     Account acct1 = createAccount(genAcctNameLocalPart("1"), attrs1);
/*      */     
/*  290 */     Map<String, Object> attrs2 = Maps.newHashMap();
/*  291 */     attrs2.put("zimbraSharedItem", "blahgranteeType:pub blah");
/*  292 */     Account acct2 = createAccount(genAcctNameLocalPart("2"), attrs2);
/*      */     
/*  294 */     Map<String, Object> attrs3 = Maps.newHashMap();
/*  295 */     attrs3.put("zimbraSharedItem", "blahgranteeType:all blah");
/*  296 */     Account acct3 = createAccount(genAcctNameLocalPart("3"), attrs3);
/*      */     
/*  298 */     SearchAccountsOptions searchOpts = new SearchAccountsOptions(new String[] { "zimbraId", "displayName", "zimbraSharedItem" });
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  304 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().accountsByGrants(GRANTEE_IDS, true, false);
/*  305 */     searchOpts.setFilter(filter);
/*  306 */     searchOpts.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  307 */     List<NamedEntry> accounts = prov.searchDirectory(searchOpts);
/*      */     
/*  309 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct1, acct2 }), accounts, true);
/*      */     
/*  311 */     deleteAccount(acct1);
/*  312 */     deleteAccount(acct2);
/*  313 */     deleteAccount(acct3);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void accountsOnServerAndCosHasSubordinates()
/*      */     throws Exception
/*      */   {
/*  322 */     SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.SUBORDINATES_OPERTIONAL_ATTRIBUTE);
/*      */     
/*  324 */     String COS_ID = prov.get(Key.CosBy.name, "default").getId();
/*      */     
/*  326 */     Account acct = createAccount(genAcctNameLocalPart());
/*  327 */     Signature sig1 = prov.createSignature(acct, "sig1", new HashMap());
/*  328 */     Signature sig2 = prov.createSignature(acct, "sig2", new HashMap());
/*      */     
/*  330 */     SearchAccountsOptions searchOpts = new SearchAccountsOptions();
/*  331 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().accountsOnServerAndCosHasSubordinates(prov.getLocalServer().getServiceHostname(), COS_ID);
/*      */     
/*  333 */     searchOpts.setFilter(filter);
/*  334 */     List<NamedEntry> accounts = prov.searchDirectory(searchOpts);
/*      */     
/*  336 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct }), accounts, false);
/*      */     
/*  338 */     deleteAccount(acct);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void CMBSearchAccountsOnly() throws Exception {
/*  343 */     Account acct1 = createAccount(genAcctNameLocalPart("1"));
/*      */     
/*  345 */     Map<String, Object> acct2Attrs = Maps.newHashMap();
/*  346 */     acct2Attrs.put("zimbraExcludeFromCMBSearch", "TRUE");
/*  347 */     Account acct2 = createAccount(genAcctNameLocalPart("2"), acct2Attrs);
/*      */     
/*  349 */     Map<String, Object> acct3Attrs = Maps.newHashMap();
/*  350 */     acct3Attrs.put("zimbraExcludeFromCMBSearch", "FALSE");
/*  351 */     Account acct3 = createAccount(genAcctNameLocalPart("3"), acct3Attrs);
/*      */     
/*  353 */     String[] returnAttrs = { "displayName", "zimbraId", "uid", "zimbraArchiveAccount", "zimbraMailHost" };
/*      */     
/*      */ 
/*      */ 
/*  357 */     SearchAccountsOptions searchOpts = new SearchAccountsOptions(domain, returnAttrs);
/*  358 */     searchOpts.setIncludeType(SearchAccountsOptions.IncludeType.ACCOUNTS_ONLY);
/*  359 */     searchOpts.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_DESCENDING);
/*  360 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().CMBSearchAccountsOnly();
/*  361 */     searchOpts.setFilter(filter);
/*  362 */     List<NamedEntry> accounts = prov.searchDirectory(searchOpts);
/*      */     
/*  364 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct3, acct1 }), accounts, true);
/*      */     
/*  366 */     deleteAccount(acct1);
/*  367 */     deleteAccount(acct2);
/*  368 */     deleteAccount(acct3);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void CMBSearchAccountsOnlyWithArchive()
/*      */     throws Exception
/*      */   {
/*  394 */     Account acct1 = createAccount(genAcctNameLocalPart("1"));
/*      */     
/*  396 */     Map<String, Object> acct2Attrs = Maps.newHashMap();
/*  397 */     acct2Attrs.put("zimbraExcludeFromCMBSearch", "TRUE");
/*  398 */     Account acct2 = createAccount(genAcctNameLocalPart("2"), acct2Attrs);
/*      */     
/*  400 */     Map<String, Object> acct3Attrs = Maps.newHashMap();
/*  401 */     acct3Attrs.put("zimbraExcludeFromCMBSearch", "FALSE");
/*  402 */     Account acct3 = createAccount(genAcctNameLocalPart("3"), acct3Attrs);
/*      */     
/*  404 */     Map<String, Object> acct4Attrs = Maps.newHashMap();
/*  405 */     acct4Attrs.put("zimbraExcludeFromCMBSearch", "FALSE");
/*  406 */     acct4Attrs.put("zimbraArchiveAccount", "archive@test.com");
/*  407 */     Account acct4 = createAccount(genAcctNameLocalPart("4"), acct4Attrs);
/*      */     
/*  409 */     String[] returnAttrs = { "displayName", "zimbraId", "uid", "zimbraArchiveAccount", "zimbraMailHost" };
/*      */     
/*      */ 
/*      */ 
/*  413 */     SearchAccountsOptions searchOpts = new SearchAccountsOptions(domain, returnAttrs);
/*  414 */     searchOpts.setIncludeType(SearchAccountsOptions.IncludeType.ACCOUNTS_ONLY);
/*  415 */     searchOpts.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_DESCENDING);
/*  416 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().CMBSearchAccountsOnlyWithArchive();
/*  417 */     searchOpts.setFilter(filter);
/*  418 */     List<NamedEntry> accounts = prov.searchDirectory(searchOpts);
/*      */     
/*  420 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct4 }), accounts, true);
/*      */     
/*  422 */     deleteAccount(acct1);
/*  423 */     deleteAccount(acct2);
/*  424 */     deleteAccount(acct3);
/*  425 */     deleteAccount(acct4);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void CMBSearchNonSystemResourceAccountsOnly()
/*      */     throws Exception
/*      */   {
/*  450 */     Account acct1 = createAccount(genAcctNameLocalPart("1"));
/*      */     
/*  452 */     Map<String, Object> acct2Attrs = Maps.newHashMap();
/*  453 */     acct2Attrs.put("zimbraExcludeFromCMBSearch", "TRUE");
/*  454 */     Account acct2 = createAccount(genAcctNameLocalPart("2"), acct2Attrs);
/*      */     
/*  456 */     Map<String, Object> acct3Attrs = Maps.newHashMap();
/*  457 */     acct3Attrs.put("zimbraExcludeFromCMBSearch", "FALSE");
/*  458 */     Account acct3 = createAccount(genAcctNameLocalPart("3"), acct3Attrs);
/*      */     
/*  460 */     Map<String, Object> acct4Attrs = Maps.newHashMap();
/*  461 */     acct4Attrs.put("zimbraExcludeFromCMBSearch", "FALSE");
/*  462 */     acct4Attrs.put("zimbraIsSystemResource", "TRUE");
/*  463 */     Account acct4 = createAccount(genAcctNameLocalPart("4"), acct4Attrs);
/*      */     
/*  465 */     Map<String, Object> acct5Attrs = Maps.newHashMap();
/*  466 */     acct5Attrs.put("zimbraExcludeFromCMBSearch", "FALSE");
/*  467 */     acct5Attrs.put("zimbraIsSystemResource", "FALSE");
/*  468 */     Account acct5 = createAccount(genAcctNameLocalPart("5"), acct5Attrs);
/*      */     
/*      */ 
/*  471 */     String[] returnAttrs = { "displayName", "zimbraId", "uid", "zimbraArchiveAccount", "zimbraMailHost" };
/*      */     
/*      */ 
/*      */ 
/*  475 */     SearchAccountsOptions searchOpts = new SearchAccountsOptions(domain, returnAttrs);
/*  476 */     searchOpts.setIncludeType(SearchAccountsOptions.IncludeType.ACCOUNTS_ONLY);
/*  477 */     searchOpts.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_DESCENDING);
/*  478 */     ZLdapFilter filter = ZLdapFilterFactory.getInstance().CMBSearchNonSystemResourceAccountsOnly();
/*  479 */     searchOpts.setFilter(filter);
/*  480 */     List<NamedEntry> accounts = prov.searchDirectory(searchOpts);
/*      */     
/*  482 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct5, acct3, acct1 }), accounts, true);
/*      */     
/*  484 */     deleteAccount(acct1);
/*  485 */     deleteAccount(acct2);
/*  486 */     deleteAccount(acct3);
/*  487 */     deleteAccount(acct4);
/*  488 */     deleteAccount(acct5);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void sslClientCertPrincipalMap()
/*      */     throws Exception
/*      */   {
/*  514 */     Account acct1 = createAccount("sslClientCertPrincipalMap-1");
/*      */     
/*  516 */     String filterStr = "(uid=sslClientCertPrincipalMap*)";
/*      */     
/*  518 */     SearchAccountsOptions searchOpts = new SearchAccountsOptions();
/*  519 */     searchOpts.setMaxResults(1);
/*  520 */     searchOpts.setFilterString(ZLdapFilterFactory.FilterId.ACCOUNT_BY_SSL_CLENT_CERT_PRINCIPAL_MAP, filterStr);
/*  521 */     List<NamedEntry> accounts = prov.searchDirectory(searchOpts);
/*      */     
/*  523 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct1 }), accounts, false);
/*      */     
/*      */ 
/*  526 */     Account acct2 = createAccount("sslClientCertPrincipalMap-2");
/*      */     
/*  528 */     searchOpts = new SearchAccountsOptions();
/*  529 */     searchOpts.setMaxResults(1);
/*  530 */     searchOpts.setFilterString(ZLdapFilterFactory.FilterId.ACCOUNT_BY_SSL_CLENT_CERT_PRINCIPAL_MAP, filterStr);
/*      */     
/*  532 */     boolean caughtTooManySearchResultsException = false;
/*      */     try {
/*  534 */       accounts = prov.searchDirectory(searchOpts);
/*      */     } catch (ServiceException e) {
/*  536 */       if ("account.TOO_MANY_SEARCH_RESULTS".equals(e.getCode())) {
/*  537 */         caughtTooManySearchResultsException = true;
/*      */       }
/*      */     }
/*  540 */     Assert.assertTrue(caughtTooManySearchResultsException);
/*      */     
/*  542 */     deleteAccount(acct1);
/*  543 */     deleteAccount(acct2);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void searchGrantee()
/*      */     throws Exception
/*      */   {
/*  574 */     String granteeId = LdapUtil.generateUUID();
/*  575 */     byte gtype = 1;
/*      */     
/*  577 */     SearchDirectoryOptions opts = new SearchDirectoryOptions();
/*  578 */     if (gtype == 1) {
/*  579 */       opts.addType(SearchDirectoryOptions.ObjectType.accounts);
/*  580 */       opts.addType(SearchDirectoryOptions.ObjectType.resources);
/*  581 */     } else if (gtype == 2) {
/*  582 */       opts.addType(SearchDirectoryOptions.ObjectType.distributionlists);
/*  583 */     } else if (gtype == 5) {
/*  584 */       opts.addType(SearchDirectoryOptions.ObjectType.coses);
/*  585 */     } else if (gtype == 4) {
/*  586 */       opts.addType(SearchDirectoryOptions.ObjectType.domains);
/*      */     } else {
/*  588 */       throw ServiceException.INVALID_REQUEST("invalid grantee type for revokeOrphanGrants", null);
/*      */     }
/*      */     
/*  591 */     String query = "(zimbraId=" + granteeId + ")";
/*  592 */     opts.setFilterString(ZLdapFilterFactory.FilterId.SEARCH_GRANTEE, query);
/*  593 */     opts.setOnMaster(true);
/*      */     
/*  595 */     Provisioning prov = Provisioning.getInstance();
/*  596 */     List<NamedEntry> entries = prov.searchDirectory(opts);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   public void getAllCalendarResources()
/*      */     throws Exception
/*      */   {
/*  635 */     Account acct = createAccount(genAcctNameLocalPart("acct"));
/*  636 */     CalendarResource cr = createCalendarResource(genAcctNameLocalPart("cr"));
/*      */     
/*  638 */     List<CalendarResource> crs = prov.getAllCalendarResources(domain);
/*      */     
/*  640 */     Verify.verifyEquals(Lists.newArrayList(new CalendarResource[] { cr }), crs, true);
/*      */     
/*  642 */     deleteAccount(acct);
/*  643 */     deleteAccount(cr);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void getAllCalendarResourcesOnServer() throws Exception {
/*  648 */     Account acct = createAccount(genAcctNameLocalPart("acct"));
/*  649 */     CalendarResource cr = createCalendarResource(genAcctNameLocalPart("cr"));
/*      */     
/*  651 */     final List<NamedEntry> crs = Lists.newArrayList();
/*  652 */     NamedEntry.Visitor visitor = new NamedEntry.Visitor()
/*      */     {
/*      */       public void visit(NamedEntry entry) throws ServiceException {
/*  655 */         crs.add(entry);
/*      */       }
/*      */       
/*  658 */     };
/*  659 */     Server server = prov.getLocalServer();
/*  660 */     prov.getAllCalendarResources(domain, server, visitor);
/*  661 */     Verify.verifyEquals(Lists.newArrayList(new CalendarResource[] { cr }), crs, false);
/*      */     
/*  663 */     crs.clear();
/*  664 */     Server otherServer = createServer(genServerName());
/*  665 */     Assert.assertNotNull(otherServer);
/*  666 */     prov.getAllCalendarResources(domain, otherServer, visitor);
/*  667 */     Verify.verifyEquals(null, crs, false);
/*      */     
/*  669 */     deleteAccount(acct);
/*  670 */     deleteAccount(cr);
/*  671 */     deleteServer(otherServer);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void getAllDistributionLists() throws Exception {
/*  676 */     DistributionList dl = createDistributionList(genGroupNameLocalPart());
/*      */     
/*  678 */     List<DistributionList> dls = prov.getAllDistributionLists(domain);
/*  679 */     Verify.verifyEquals(Lists.newArrayList(new DistributionList[] { dl }), dls, true);
/*      */     
/*  681 */     deleteGroup(dl);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void getAllGroups() throws Exception {
/*  686 */     DistributionList dl = createDistributionList(genGroupNameLocalPart("dl"));
/*  687 */     DynamicGroup dg = createDynamicGroup(genGroupNameLocalPart("dg"));
/*      */     
/*      */ 
/*  690 */     String SUB_DOMAIN_NAME = "sub." + baseDomainName();
/*  691 */     Domain subDomain = provUtil.createDomain(SUB_DOMAIN_NAME, null);
/*      */     
/*      */ 
/*  694 */     DistributionList dlSub = createDistributionList(genGroupNameLocalPart("dl-sub"), subDomain);
/*  695 */     DynamicGroup dgSub = createDynamicGroup(genGroupNameLocalPart("dg-sub"), subDomain);
/*      */     
/*  697 */     List<Group> groups = prov.getAllGroups(domain);
/*      */     
/*  699 */     Verify.verifyEquals(Lists.newArrayList(new Group[] { dg, dl }), groups, true);
/*      */     
/*  701 */     deleteGroup(dl);
/*  702 */     deleteGroup(dg);
/*  703 */     deleteGroup(dlSub);
/*  704 */     deleteGroup(dgSub);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void renameDomainSearchAcctCrDl() throws Exception {
/*  709 */     Account acct = createAccount(genAcctNameLocalPart("acct"));
/*  710 */     CalendarResource cr = createCalendarResource(genAcctNameLocalPart("cr"));
/*  711 */     DistributionList dl = createDistributionList(genGroupNameLocalPart("dl"));
/*      */     
/*  713 */     String domainDN = ((LdapDomain)domain).getDN();
/*  714 */     String searchBase = ((LdapProv)prov).getDIT().domainDNToAccountSearchDN(domainDN);
/*      */     
/*  716 */     final List<NamedEntry> entries = Lists.newArrayList();
/*  717 */     NamedEntry.Visitor visitor = new NamedEntry.Visitor()
/*      */     {
/*      */       public void visit(NamedEntry entry) throws ServiceException
/*      */       {
/*  721 */         entries.add(entry);
/*      */       }
/*      */       
/*  724 */     };
/*  725 */     SearchDirectoryOptions options = new SearchDirectoryOptions();
/*  726 */     options.setDomain(domain);
/*  727 */     options.setOnMaster(true);
/*  728 */     options.setFilterString(ZLdapFilterFactory.FilterId.RENAME_DOMAIN, null);
/*  729 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts, SearchDirectoryOptions.ObjectType.resources, SearchDirectoryOptions.ObjectType.distributionlists });
/*  730 */     prov.searchDirectory(options, visitor);
/*  731 */     Verify.verifyEquals(Lists.newArrayList(new MailTarget[] { acct, cr, dl }), entries, false);
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  743 */     deleteAccount(acct);
/*  744 */     deleteAccount(cr);
/*  745 */     deleteGroup(dl);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void searchAliasTarget() throws Exception {
/*  750 */     Account acct = createAccount(genAcctNameLocalPart("acct"));
/*  751 */     CalendarResource cr = createCalendarResource(genAcctNameLocalPart("cr"));
/*  752 */     DistributionList dl = createDistributionList(genGroupNameLocalPart("dl"));
/*  753 */     DynamicGroup dg = createDynamicGroup(genGroupNameLocalPart("dg"));
/*      */     
/*      */ 
/*  756 */     prov.addAlias(acct, TestUtil.getAddress("1-acct-alias", domain.getName()));
/*  757 */     prov.addAlias(cr, TestUtil.getAddress("2-cr-alias", domain.getName()));
/*  758 */     prov.addGroupAlias(dl, TestUtil.getAddress("3-dl-alias", domain.getName()));
/*  759 */     prov.addGroupAlias(dg, TestUtil.getAddress("4-dg-alias", domain.getName()));
/*      */     
/*  761 */     SearchDirectoryOptions options = new SearchDirectoryOptions(domain);
/*  762 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.aliases });
/*  763 */     options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  764 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, null);
/*  765 */     List<NamedEntry> aliases = prov.searchDirectory(options);
/*      */     
/*  767 */     Assert.assertEquals(4L, aliases.size());
/*      */     
/*  769 */     Alias acctAlias = (Alias)aliases.get(0);
/*  770 */     Alias crAlias = (Alias)aliases.get(1);
/*  771 */     Alias dlAlias = (Alias)aliases.get(2);
/*  772 */     Alias dgAlias = (Alias)aliases.get(3);
/*      */     
/*  774 */     NamedEntry acctAliasTarget = prov.searchAliasTarget(acctAlias, true);
/*  775 */     Assert.assertEquals(acct.getId(), acctAliasTarget.getId());
/*      */     
/*  777 */     NamedEntry crAliasTarget = prov.searchAliasTarget(crAlias, true);
/*  778 */     Assert.assertEquals(cr.getId(), crAliasTarget.getId());
/*      */     
/*  780 */     NamedEntry dlAliasTarget = prov.searchAliasTarget(dlAlias, true);
/*  781 */     Assert.assertEquals(dl.getId(), dlAliasTarget.getId());
/*      */     
/*  783 */     NamedEntry dgAliasTarget = prov.searchAliasTarget(dgAlias, true);
/*  784 */     Assert.assertEquals(dg.getId(), dgAliasTarget.getId());
/*      */     
/*  786 */     deleteAccount(acct);
/*  787 */     deleteAccount(cr);
/*  788 */     deleteGroup(dl);
/*  789 */     deleteGroup(dg);
/*      */   }
/*      */   
/*      */   @Test
/*      */   @QA.Bug(bug={67379})
/*      */   public void wildcardFilter() throws Exception {
/*  795 */     Account acct1 = createAccount(genAcctNameLocalPart("1"));
/*  796 */     Account acct2 = createAccount(genAcctNameLocalPart("2"));
/*  797 */     Account acct3 = createAccount(genAcctNameLocalPart("3"));
/*      */     
/*  799 */     SearchDirectoryOptions options = new SearchDirectoryOptions(domain);
/*  800 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts });
/*  801 */     options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  802 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, "(cn=*)");
/*  803 */     options.setConvertIDNToAscii(true);
/*  804 */     List<NamedEntry> entries = prov.searchDirectory(options);
/*      */     
/*  806 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct1, acct2, acct3 }), entries, true);
/*      */     
/*  808 */     deleteAccount(acct1);
/*  809 */     deleteAccount(acct2);
/*  810 */     deleteAccount(acct3);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void chineseFilter() throws Exception
/*      */   {
/*  816 */     Map<String, Object> acct1Attrs = Maps.newHashMap();
/*  817 */     acct1Attrs.put("cn", "中文 1");
/*  818 */     Account acct1 = createAccount(genAcctNameLocalPart("1"), acct1Attrs);
/*      */     
/*  820 */     Map<String, Object> acct2Attrs = Maps.newHashMap();
/*  821 */     acct2Attrs.put("cn", "中文 2");
/*  822 */     Account acct2 = createAccount(genAcctNameLocalPart("2"), acct2Attrs);
/*      */     
/*  824 */     Map<String, Object> acct3Attrs = Maps.newHashMap();
/*  825 */     acct3Attrs.put("cn", "中文 3");
/*  826 */     Account acct3 = createAccount(genAcctNameLocalPart("3"), acct3Attrs);
/*      */     
/*  828 */     SearchDirectoryOptions options = new SearchDirectoryOptions(domain);
/*  829 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts });
/*  830 */     options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  831 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, "(cn=中文 2)");
/*  832 */     options.setConvertIDNToAscii(true);
/*  833 */     List<NamedEntry> entries = prov.searchDirectory(options);
/*      */     
/*  835 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct2 }), entries, true);
/*      */     
/*  837 */     deleteAccount(acct1);
/*  838 */     deleteAccount(acct2);
/*  839 */     deleteAccount(acct3);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void idnFilter() throws Exception {
/*  844 */     String idnDomainUnicodeName = "中文." + baseDomainName();
/*  845 */     Domain idnDomain = provUtil.createDomain(idnDomainUnicodeName);
/*      */     
/*  847 */     String acctNameLocalPart = genAcctNameLocalPart();
/*  848 */     Account acct = provUtil.createAccount(acctNameLocalPart, idnDomain);
/*  849 */     String acctUnicodeName = acctNameLocalPart + "@" + idnDomainUnicodeName;
/*      */     
/*  851 */     SearchDirectoryOptions options = new SearchDirectoryOptions(idnDomain);
/*  852 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts });
/*  853 */     options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  854 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, String.format("(zimbraMailDeliveryAddress=%s)", new Object[] { acctUnicodeName }));
/*      */     
/*  856 */     options.setConvertIDNToAscii(true);
/*  857 */     List<NamedEntry> entries = prov.searchDirectory(options);
/*      */     
/*  859 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct }), entries, true);
/*      */     
/*  861 */     deleteAccount(acct);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void maxResults() throws Exception {
/*  866 */     int NUM_ACCTS = 3;
/*      */     
/*  868 */     List<Account> accts = Lists.newArrayList();
/*  869 */     for (int i = 0; i < NUM_ACCTS; i++) {
/*  870 */       Map<String, Object> attrs = Maps.newHashMap();
/*  871 */       attrs.put("displayName", "acct-" + i);
/*      */       
/*  873 */       String ACCT_LOCALPART = Names.makeAccountNameLocalPart("acct-" + i);
/*  874 */       Account acct = createAccount(ACCT_LOCALPART, attrs);
/*  875 */       accts.add(acct);
/*      */     }
/*      */     
/*  878 */     String filter = "(uid=*)";
/*  879 */     String sortAttr = "displayName";
/*      */     
/*  881 */     String[] returnAttrs = { "displayName", "zimbraId", "zimbraMailHost", "uid", "zimbraAccountStatus", "zimbraIsAdminAccount", "zimbraMailStatus" };
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*  891 */     SearchDirectoryOptions searchOpts = new SearchDirectoryOptions();
/*  892 */     searchOpts.setDomain(domain);
/*  893 */     searchOpts.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts });
/*  894 */     searchOpts.setMaxResults(0);
/*  895 */     searchOpts.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, filter);
/*  896 */     searchOpts.setReturnAttrs(returnAttrs);
/*  897 */     searchOpts.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  898 */     searchOpts.setSortAttr(sortAttr);
/*      */     
/*  900 */     List<NamedEntry> result = prov.searchDirectory(searchOpts);
/*  901 */     Verify.verifyEquals(accts, result, true);
/*      */     
/*  903 */     searchOpts.setMaxResults(NUM_ACCTS - 1);
/*  904 */     boolean caughtException = false;
/*      */     try {
/*  906 */       prov.searchDirectory(searchOpts);
/*      */     } catch (ServiceException e) {
/*  908 */       if ("account.TOO_MANY_SEARCH_RESULTS".equals(e.getCode())) {
/*  909 */         caughtException = true;
/*      */       }
/*      */     }
/*  912 */     Assert.assertTrue(caughtException);
/*      */     
/*  914 */     for (Account acct : accts) {
/*  915 */       deleteAccount(acct);
/*      */     }
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void dnSubtreeMatchFilter() throws Exception {
/*  921 */     String SUB_DOMAIN_BASE = genDomainSegmentName() + "." + baseDomainName();
/*      */     
/*  923 */     String SUB_DOMAIN_NAME = "sub." + SUB_DOMAIN_BASE;
/*  924 */     Domain subDomain = provUtil.createDomain(SUB_DOMAIN_NAME);
/*      */     
/*  926 */     String SUB_SUB_DOMAIN_NAME = "sub." + SUB_DOMAIN_NAME;
/*  927 */     Domain subSubDomain = provUtil.createDomain(SUB_SUB_DOMAIN_NAME);
/*      */     
/*      */ 
/*  930 */     Account acct = provUtil.createAccount(genAcctNameLocalPart("acct"), subDomain);
/*  931 */     DistributionList dl = provUtil.createDistributionList(genGroupNameLocalPart("dl"), subDomain);
/*  932 */     DynamicGroup dg = provUtil.createDynamicGroup(genGroupNameLocalPart("dg"), subDomain);
/*      */     
/*      */ 
/*  935 */     Account acctSub = provUtil.createAccount(genAcctNameLocalPart("acct"), subSubDomain);
/*  936 */     DistributionList dlSub = provUtil.createDistributionList(genGroupNameLocalPart("dl"), subSubDomain);
/*  937 */     DynamicGroup dgSub = provUtil.createDynamicGroup(genGroupNameLocalPart("dg"), subSubDomain);
/*      */     
/*      */ 
/*      */ 
/*      */     try
/*      */     {
/*  943 */       SKIP_FOR_INMEM_LDAP_SERVER(ProvTest.SkipTestReason.DN_SUBTREE_MATCH_FILTER);
/*      */       
/*      */ 
/*  946 */       options = new SearchDirectoryOptions();
/*  947 */       options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts, SearchDirectoryOptions.ObjectType.distributionlists, SearchDirectoryOptions.ObjectType.dynamicgroups });
/*  948 */       options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  949 */       options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, ((LdapDomain)subDomain).getDnSubtreeMatchFilter().toFilterString());
/*  950 */       entries = prov.searchDirectory(options);
/*      */       
/*  952 */       Verify.verifyEquals(Lists.newArrayList(new MailTarget[] { acct, dg, dl }), entries, true);
/*      */     }
/*      */     catch (ProvTest.SkippedForInMemLdapServerException e) {}
/*      */     
/*      */ 
/*  957 */     SearchDirectoryOptions options = new SearchDirectoryOptions(subDomain);
/*  958 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts, SearchDirectoryOptions.ObjectType.distributionlists, SearchDirectoryOptions.ObjectType.dynamicgroups });
/*  959 */     options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  960 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, null);
/*  961 */     List<NamedEntry> entries = prov.searchDirectory(options);
/*      */     
/*  963 */     Verify.verifyEquals(Lists.newArrayList(new MailTarget[] { acct, dg, dl }), entries, true);
/*      */     
/*      */ 
/*  966 */     options = new SearchDirectoryOptions(subDomain);
/*  967 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts, SearchDirectoryOptions.ObjectType.distributionlists });
/*  968 */     options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  969 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, null);
/*  970 */     entries = prov.searchDirectory(options);
/*      */     
/*  972 */     Verify.verifyEquals(Lists.newArrayList(new MailTarget[] { acct, dl }), entries, true);
/*      */     
/*      */ 
/*  975 */     options = new SearchDirectoryOptions(subDomain);
/*  976 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.dynamicgroups });
/*  977 */     options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  978 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, null);
/*  979 */     entries = prov.searchDirectory(options);
/*      */     
/*  981 */     Verify.verifyEquals(Lists.newArrayList(new DynamicGroup[] { dg }), entries, true);
/*      */     
/*      */ 
/*  984 */     options = new SearchDirectoryOptions(subDomain);
/*  985 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts, SearchDirectoryOptions.ObjectType.dynamicgroups });
/*  986 */     options.setSortOpt(SearchDirectoryOptions.SortOpt.SORT_ASCENDING);
/*  987 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, null);
/*  988 */     entries = prov.searchDirectory(options);
/*      */     
/*  990 */     Verify.verifyEquals(Lists.newArrayList(new MailTarget[] { acct, dg }), entries, true);
/*      */     
/*      */ 
/*  993 */     deleteAccount(acct);
/*  994 */     deleteGroup(dl);
/*  995 */     deleteGroup(dg);
/*  996 */     deleteAccount(acctSub);
/*  997 */     deleteGroup(dlSub);
/*  998 */     deleteGroup(dgSub);
/*  999 */     deleteDomain(subSubDomain);
/* 1000 */     deleteDomain(subDomain);
/*      */   }
/*      */   
/*      */   @Test
/*      */   @QA.Bug(bug={68964})
/*      */   public void filterWithTrailingDot() throws Exception
/*      */   {
/* 1007 */     Domain subDomain = provUtil.createDomain(genDomainName(baseDomainName()));
/* 1008 */     Account acct = provUtil.createAccount(genAcctNameLocalPart(), subDomain);
/*      */     
/* 1010 */     SearchDirectoryOptions options = new SearchDirectoryOptions();
/* 1011 */     options.setDomain(subDomain);
/* 1012 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts });
/* 1013 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, "(zimbraMailDeliveryAddress=*.*)");
/* 1014 */     options.setReturnAttrs(new String[] { "zimbraMailDeliveryAddress" });
/* 1015 */     options.setConvertIDNToAscii(true);
/*      */     
/* 1017 */     List<NamedEntry> entries = prov.searchDirectory(options);
/* 1018 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { acct }), entries, true);
/*      */     
/* 1020 */     deleteAccount(acct);
/* 1021 */     deleteDomain(subDomain);
/*      */   }
/*      */   
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */   @Test
/*      */   @QA.Bug(bug={68965})
/*      */   public void filterWithCharsNeedEscaping()
/*      */     throws Exception
/*      */   {
/* 1045 */     Domain subDomain = provUtil.createDomain(genDomainName(baseDomainName()));
/*      */     
/* 1047 */     String ATTR = "displayName";
/*      */     
/* 1049 */     Account acctStar = provUtil.createAccount(genAcctNameLocalPart("star"), subDomain, Collections.singletonMap(ATTR, "*"));
/*      */     
/* 1051 */     Account acctLeftParen = provUtil.createAccount(genAcctNameLocalPart("left-paren"), subDomain, Collections.singletonMap(ATTR, "("));
/*      */     
/* 1053 */     Account acctRightParen = provUtil.createAccount(genAcctNameLocalPart("right-paren"), subDomain, Collections.singletonMap(ATTR, ")"));
/*      */     
/* 1055 */     Account acctBackSlash = provUtil.createAccount(genAcctNameLocalPart("back-slash"), subDomain, Collections.singletonMap(ATTR, "\\"));
/*      */     
/*      */ 
/* 1058 */     testFilterWithCharsNeedEscaping(acctStar, ATTR, "*\\2a*");
/* 1059 */     testFilterWithCharsNeedEscaping(acctLeftParen, ATTR, "*\\28*");
/* 1060 */     testFilterWithCharsNeedEscaping(acctRightParen, ATTR, "*\\29*");
/* 1061 */     testFilterWithCharsNeedEscaping(acctBackSlash, ATTR, "*\\5c*");
/*      */     
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/*      */ 
/* 1072 */     deleteAccount(acctStar);
/* 1073 */     deleteAccount(acctLeftParen);
/* 1074 */     deleteAccount(acctRightParen);
/* 1075 */     deleteAccount(acctBackSlash);
/* 1076 */     deleteDomain(subDomain);
/*      */   }
/*      */   
/*      */   private void testFilterWithCharsNeedEscaping(Account expected, String filterAttr, String filterValue) throws Exception
/*      */   {
/* 1081 */     SearchDirectoryOptions options = new SearchDirectoryOptions();
/* 1082 */     options.setDomain(prov.getDomain(expected));
/* 1083 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts });
/* 1084 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, String.format("(%s=%s)", new Object[] { filterAttr, filterValue }));
/* 1085 */     options.setReturnAttrs(new String[] { "zimbraMailDeliveryAddress" });
/* 1086 */     options.setConvertIDNToAscii(true);
/*      */     
/* 1088 */     List<NamedEntry> entries = prov.searchDirectory(options);
/* 1089 */     Verify.verifyEquals(Lists.newArrayList(new Account[] { expected }), entries, true);
/*      */   }
/*      */   
/*      */   @Test
/*      */   public void badFilter() throws Exception {
/* 1094 */     testBadFilter("(objectClass=**)");
/* 1095 */     testBadFilter("(objectClass=***)");
/* 1096 */     testBadFilter("(objectClass=()");
/* 1097 */     testBadFilter("(objectClass=))");
/* 1098 */     testBadFilter("(objectClass=\\)");
/*      */   }
/*      */   
/*      */   private void testBadFilter(String filter) throws Exception {
/* 1102 */     SearchDirectoryOptions options = new SearchDirectoryOptions();
/* 1103 */     options.setTypes(new SearchDirectoryOptions.ObjectType[] { SearchDirectoryOptions.ObjectType.accounts });
/* 1104 */     options.setFilterString(ZLdapFilterFactory.FilterId.UNITTEST, filter);
/* 1105 */     options.setReturnAttrs(new String[] { "zimbraId" });
/* 1106 */     options.setConvertIDNToAscii(true);
/*      */     
/* 1108 */     String errorCode = null;
/*      */     try {
/* 1110 */       prov.searchDirectory(options);
/*      */     } catch (ServiceException e) {
/* 1112 */       errorCode = e.getCode();
/*      */     }
/* 1114 */     Assert.assertEquals("ldap.INVALID_SEARCH_FILTER", errorCode);
/*      */   }
/*      */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapProvSearchDirectory.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */